Skip to content
This repository was archived by the owner on Sep 15, 2026. It is now read-only.

fix: typings now export class instead of namespace - #88

Merged
dylanirlbeck merged 3 commits into
draftbit:masterfrom
fdebijl:master
Apr 27, 2020
Merged

dylanirlbeck merged 3 commits into
draftbit:masterfrom
fdebijl:master

Conversation

@fdebijl

@fdebijl fdebijl commented Apr 24, 2020 •

Copy link
Copy Markdown
Contributor

The typings now follow the handbook's recommended definition for modules that export a class.

Following this PR, the safe import signature (which will work without esModuleInterop) is

const Twitter = require('twitter-lite')

If esModuleInterop is enabled, you can also use the import ... from signature, optionally with typings:

import Twitter from 'twitter-lite';
import Twitter, { TwitterOptions, AccessTokenOptions } from 'twitter-lite';

Note that the const ... require signature doesn't allow you to import typings from twitter-lite, so I would recommend using esModuleInterop in any event.

Fixes #83

@dylanirlbeck dylanirlbeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks so much for doing this. Can you fix the merge conflicts? We should be good to merge after that.

@fdebijl

fdebijl commented Apr 24, 2020

Copy link
Copy Markdown
Contributor Author

Maybe @danbockapps can verify it works on his machine before we merge, I might have overlooked something (again)

@danbockapps

Copy link
Copy Markdown

The sample code at the top of index.d.ts compiles, so that's good. I didn't test all the features.

I'm curious to know how you develop and test the index.d.ts file. Is there an ad-hoc TS script that you're using? What directory does it go in and how does it import the module?

@fdebijl

fdebijl commented Apr 25, 2020 •

Copy link
Copy Markdown
Contributor Author

@danbockapps Good to hear! I use a blank project with the following tsconfig

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "alwaysStrict": true
  }
}

This used to also contain esModuleInterop: true, hence the previous typings not working as intended for people who don't have it enabled.

The project has the latest release of twitter-lite installed as a dependency so I can work on the typings (inside node_modules/twitter-lite/dist/index.d.ts, but from the next release onward in node_modules/twitter-lite/index.d.ts) as they would be distributed. In src/index.ts

I have a file containing

import Twitter = require('twitter-lite');

const twitter = new Twitter({
  access_token_key: 'XYZ',
  access_token_secret: 'XYZ',
  consumer_key: 'XYZ',
  consumer_secret: 'XYZ'
})

@maybeanerd

Copy link
Copy Markdown
Contributor

Hey @fdebijl , I just wanted to drop in and notify you that with #85 , changes were made to the types and have already been merged into master.
If you can make sure the master is merged into this branch correctly, it would be great!
Since you moved the class definition, I don't think this would be an automatic merge.

@fdebijl

fdebijl commented Apr 25, 2020

Copy link
Copy Markdown
Contributor Author

@T0TProduction the changes in #85 are merged into this branch

@maybeanerd maybeanerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fdebijl according to the GitHub change overview, this PR will remove many things that were just introduced into master, and also removes changes from #85
some (maybe all, maybe not all) examples are marked by my comments in this review:

Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread .github/workflows/nodejs.yml Resolved

@dylanirlbeck dylanirlbeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically reiterating what @T0TProduction said, but this PR needs to cut down its scope a bit, a bunch of changes were already merged into master. Can you make sure you only include the relevant changes?

@fdebijl

fdebijl commented Apr 25, 2020

Copy link
Copy Markdown
Contributor Author

Reset to only include the typings

@maybeanerd maybeanerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two questions/remarks, the rest LGTM!

Comment thread index.d.ts Resolved
Comment thread index.d.ts Resolved
@fdebijl

fdebijl commented Apr 27, 2020

Copy link
Copy Markdown
Contributor Author

@T0TProduction I think the goal for the example is that it works on every configuration, regardless of allowSyntheticDefaultImports and esModuleInterop. Having done some further reading and research, I agree with export default and const Twitter = require('twitter-lite'):

without esModuleInterop import Twitter from 'twitter-lite' import Twitter = require('twitter-lite') const Twitter = require('twitter-lite') import Twitter, { AccessTokenOptions } from 'twitter-lite' import { Twitter } from 'twitter-lite'
export = Twitter
(w/ merged namespace)
Current code in this PR
❌ (tsc) ✅ ✅ ❌ (tsc) ❌ (tsc)
export class Twitter
(w/ merged namespace)
❌ (tsc) ❌ (tsc) ✅ ❌ (tsc) ❌ (runtime)
export default class Twitter
(no namespace)
❌ (runtime) ❌ (tsc) ✅ ❌ (runtime) ❌ (tsc)
export namespace 'TwitterLite';
Typings on master
❌ (tsc) ❌ (tsc) ✅ ❌ (tsc) ❌ (rumtime)
with esModuleInterop import Twitter from 'twitter-lite' import Twitter = require('twitter-lite') const Twitter = require('twitter-lite') import Twitter, { AccessTokenOptions } from 'twitter-lite' import { Twitter } from 'twitter-lite'
export = Twitter
(w/ merged namespace)
Current code in this PR
✅ ✅ ✅ ✅ ❌ (tsc)
export class Twitter
(w/ merged namespace)
❌ (tsc) ❌ (tsc) ✅ ❌ (tsc) ❌ (runtime)
export default class Twitter
(no namespace)
✅ ❌ (tsc) ✅ ✅ ❌ (tsc)
export namespace 'TwitterLite';
Typings on master
❌ (tsc) ❌ (tsc) ✅ ❌ (tsc) ❌ (runtime)

const Twitter = require('twitter-lite') just works across the board, which I didn't realize before. I'm amending this PR accordingly.

fdebijl added 2 commits April 27, 2020 14:24
The recommended import signature is now const Twitter = require('twitter-lite');

@dylanirlbeck dylanirlbeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes LGTM! Thanks for the thorough analysis in your comments -- I agree with your decisions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: "not a constructor" when using TypeScript

4 participants